package com.gitee.haioa;

import javax.swing.filechooser.FileSystemView;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

/**
 * @author HaiQiu
 * @since 2023/1/4
 */
public class WinShell {

    public static final String BAT = "bat";
    public static final String PRE = "/";

    public static final String SPOT = ".";

    public static final String ROOT = "/root";

    //运行脚本项目路径
    public static final String PARENT_PATH = "/opt/zhsw/backend";

    public static final String CD = "cd ";

    public static final String AND = " && ";

    public static String getDeskTopPath() {
        FileSystemView view = FileSystemView.getFileSystemView();
        File file = view.getHomeDirectory();
        System.out.println(file.getPath());
        return file.getPath();
    }


    public static void createShellFile(String fileName) {
        String deskTopPath = getDeskTopPath();
        String port = fileName.substring(0, fileName.lastIndexOf(":"));
        String fileName1 = fileName.substring(fileName.lastIndexOf(":") + 1);
        String prefix = fileName1.substring(0, fileName1.lastIndexOf("/"));
        String suffix = fileName1.substring(fileName1.lastIndexOf("/") + 1);
        String s1 = suffix.substring(0, suffix.lastIndexOf("."));
        String s2 = suffix.substring(suffix.lastIndexOf("."));
        String shellName = deskTopPath + PRE + "生成脚本" + prefix + PRE + s1 + SPOT + BAT;
        File file = new File(shellName);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            System.out.println("文件夹创建成功：" + file.getParent());
        } else {
            file.delete();
            System.out.println("删除旧文件：" + file);
            file.getParentFile().delete();
            System.out.println("删除旧文件夹：" + file.getParent());
            file.getParentFile().mkdirs();
            System.out.println("文件夹创建成功：" + file.getParent());
        }
        try {
            file.delete();
            boolean newFile = file.createNewFile();
            if (newFile) {
                System.out.println("脚本文件创建成功：" + shellName);
            }
            // write
            FileWriter fw = new FileWriter(file, true);
            BufferedWriter bw = new BufferedWriter(fw);
            StringBuffer buffer = new StringBuffer();
            buffer.append("@echo off\n")
                    .append("set port=" + port + "\n")
                    .append("set BUILD_ID=dontKillMe\n")
                    .append("for /f \"tokens=1-5\" %%i in ('netstat -ano^|findstr \":%port%\"') do (\n")
                    .append(" echo kill the process %%m who use the port \n")
                    .append(" taskkill /pid %%m -t -f\n")
                    .append(" goto start\n")
                    .append(")\n")
                    .append(":start\n")
                    .append("start \""+ s1 + "\" /min java -Dfile.encoding=utf-8 -Xmx1024m -Xms1024m -jar " + suffix +
                            " --server.port=" + port +
                            " --spring.profiles.active=pro");
            bw.write(buffer.toString());
            bw.flush();
            bw.close();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
